﻿$(document).ready(function () {
    function FixLogs() {
        var self = this;

        this.filename = ko.observable('');
        this.status = ko.observable(0);
        this.logs = ko.observableArray([]);

        this.open = function (item, e) {
            $(e.target).guard(function () {
                Local.ajax('open', self.filename(), function (data) {
                    if (data) {
                        self.filename(data);
                        self.logs([]);
                        self.start();
                    }
                });
            });
        };
        this.start = function () {
            this.status(1);
            Local.ajax('start', '', function (data) {
                self.status(0);
                window.external.Alert('Done');
            }, self, true, function (data) {
                self.logs.splice(0, 0, {
                    type: ko.observable(data.Type),
                    content: ko.observable(data.Content)
                });
            });
        };
        this.cancel = function (item, e) {
            $(e.target).guard(function () {
                Local.ajax('cancel', '');
                self.filename('');
                self.status(0);
            });
        };
    }

    var logs = new FixLogs();
    ko.applyBindings(logs);
});

$(window).load(function () {
    $(window).trigger('resize');
});